package com.example.myaccount.fragment;

import android.app.DatePickerDialog;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import com.example.myaccount.R;
import com.example.myaccount.activity.MainActivity;
import com.example.myaccount.component.PieView;
import com.example.myaccount.database.DatabaseHelper;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;

public class MonthlyIncomeFragment extends Fragment {

    private View root;
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_analysis_outcome, container, false);
        DatabaseHelper databaseHelper = DatabaseHelper.getDefaultDatabaseHelper(MainActivity.getContext());
        Button mBtnMonth = root.findViewById(R.id.btn_month);
        DateFormat df = new SimpleDateFormat("yyyy_MM_dd");
        String date = df.format(Calendar.getInstance().getTime());
        String[] dateSplit = date.split("_");
        String year = dateSplit[0];
        String month = dateSplit[1];
        mBtnMonth.setText(year+"_"+month);
        PieView pieView = root.findViewById(R.id.pieView);
        Map<String, Double> dataMap = databaseHelper.getIncomeTypeMonthlyMap(year,month);
        pieView.setmDataMap(dataMap);
        pieView.invalidate();
        mBtnMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar ca = Calendar.getInstance();
                final int mYear = ca.get(Calendar.YEAR);
                final int mMonth = ca.get(Calendar.MONTH);
                final int mDay = ca.get(Calendar.DAY_OF_MONTH);
                DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(),R.style.MyDatePickerDialogTheme,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
//                                Log.d(MyConst.TAG,year+"_"+month+"_"+dayOfMonth);
                                month=month+1;
                                String date =  year+"_"+month;
                                mBtnMonth.setText(date);
                                Map<String, Double> dataMap = databaseHelper.getIncomeTypeMonthlyMap(new Integer(year).toString(),new Integer(month).toString());
                                pieView.setmDataMap(dataMap);
                                pieView.invalidate();
                            }
                        },mYear,mMonth,mDay);
                datePickerDialog.show();
            }
        });
        return root;
    }
}
